package com.longxi.tianjintianbao.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.approval.NextNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxw on & 2018/11/5
 */
public class PropertyDialog extends Dialog {
    public PropertyDialog(@NonNull Context context) {
        super(context);
    }

    public PropertyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PropertyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private AdapterListener listener;
        String title = "未定义标题";

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            this.title = title;
            return this;
        }

        public Builder setListener(AdapterListener listener) {
            this.listener = listener;
            return this;
        }

        private RecyclerView recyclerView;
        private TextView tv_title;
        private TextView tv_cancle;
        private PropertyViewAdapter adapter;
        private PropertyDialog dialog;

        public PropertyDialog create() {
            dialog = new PropertyDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_property, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            recyclerView = view.findViewById(R.id.recyclerView);
            tv_title = view.findViewById(R.id.tv_title);
            tv_cancle = view.findViewById(R.id.tv_cancle);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            tv_cancle.setOnClickListener(v -> dialog.dismiss());
            return dialog;
        }

        public void setNextNode(List<NextNode> nextNodes){
            adapter = new PropertyViewAdapter(nextNodes);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        public class PropertyViewAdapter extends RecyclerView.Adapter<PropertyViewAdapter.ViewHolder> {

            List<NextNode> list;

            public PropertyViewAdapter(List<NextNode> list) {
                this.list = list;
            }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_property, parent, false);
                return new ViewHolder(v);
            }

            @Override
            public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                NextNode propertyCompany = list.get(position);
                holder.tv_name.setText(propertyCompany.getName());
                if(listener != null){
                    holder.tv_name.setOnClickListener(v -> listener.onClick(holder.getAdapterPosition()));
                }
            }

            @Override
            public int getItemCount() {
                return list.size();
            }

            /**
             * @author chensuilun
             */
            protected class ViewHolder extends RecyclerView.ViewHolder {
                TextView tv_name;
                public ViewHolder(View itemView) {
                    super(itemView);
                    tv_name = itemView.findViewById(R.id.tv_name);
                }
            }
        }

        public void show(){
            if(dialog != null){
                dialog.show();
            }
        }

        public void dismiss(){
            dialog.dismiss();
        }
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(25, 0, 25, 25);

        getWindow().setAttributes(layoutParams);
    }

    public interface AdapterListener {
        void onClick(int position);
    }

}
